package com.key.controller.admin.setting;

import com.key.common.R;
import com.key.common.TranException;
import com.key.common.vo.AssetModelVO;
import com.key.entity.AssetModel;
import com.key.service.AssetModelService;
import com.key.utils.CosUtils;
import com.key.utils.CosUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/*
 *
 * @author liu
 *
 */
@Controller
@RequestMapping("/asset_model")
@Api(value = "资产型号管理",tags = "资产型号管理接口API")
public class AssetModelController {
    @Autowired
    private AssetModelService assetModelService;
    @RequestMapping(value = "addAssetModel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加资产型号",notes = "添加资产型号信息",httpMethod = "post", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R addAssetModel(@RequestBody @ApiParam(name = "assetModel",value = "资产型号",required = true) AssetModel assetModel){

        return assetModelService.addAssetModel(assetModel);
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上传图片",notes = "上传图片路径",httpMethod = "post", response = R.class)
   // @ApiImplicitParam(name = "imgUrl",value = "图片路径",paramType = "formData",dataType = "file",required = true)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R upload(@RequestParam("imgUrl")  MultipartFile imgUrl,HttpServletRequest request) throws IOException {
        try {
            CosUtils.initCosClient();
            URL url = CosUtils.uploadFile(imgUrl, "key-1316841246", CosUtils.formatKey(imgUrl));
            if(!url.toString().equals("")){
                R r=R.ok();
                r.setData(url.toString());
                return r;
            }else {
                throw new TranException("上传失败");
            }
        } finally{
            CosUtils.shutdownClient();
        }


    }

    @RequestMapping(value = "/findAllAssetModel",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询所有资产型号",notes = "根据资产分类Id查询资产型号表所有信息",httpMethod = "get", response = R.class)
    @ApiImplicitParam(name = "assetTypeId",value = "资产分类Id",paramType = "query",dataType = "String",required = true)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R findAllAssetModel(@RequestParam("assetTypeId")Integer assetTypeId){

        return assetModelService.findAllAssetModelByAssetTypeId(assetTypeId);
    }

    @RequestMapping(value = "/findAssetModelById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询资产型号",notes = "查询资产型号表所有信息",httpMethod = "get", response = R.class)
    @ApiImplicitParam(name = "id",value = "资产型号Id",paramType = "query",dataType = "int",required = true)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R findAssetModelById(@RequestParam("id")Integer id){

        return assetModelService.findAllAssetModelById(id);
    }

    @RequestMapping(value = "/updateAssetModel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改资产型号",notes = "修改资产型号信息",httpMethod = "post", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R updateAssetModel(@RequestBody @ApiParam(name = "assetModel",value = "资产型号",required = true) AssetModel assetModel){

        return assetModelService.updateAssetModel(assetModel);
    }

    @RequestMapping(value = "/disableAssetModel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "禁用资产模型",notes = "修改资产模型状态",httpMethod = "post", response = R.class)
    @ApiImplicitParam(name = "id",value = "资产模型Id",paramType = "query",dataType = "int",required = true)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R disableAssetModel(@RequestParam("id") Integer id){

        return assetModelService.disableAssetModel(id);
    }
    @RequestMapping(value = "/enableAssetModel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解禁资产模型",notes = "修改资产模型状态",httpMethod = "post", response = R.class)
    @ApiImplicitParam(name = "id",value = "资产模型Id",paramType = "query",dataType = "int",required = true)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R enableAssetModel(@RequestParam("id") Integer id){

        return assetModelService.enableAssetModel(id);
    }

    @RequestMapping(value = "/deleteAssetModel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除资产模型",notes = "删除资产模型",httpMethod = "post", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R deleteAssetModel(@RequestBody @ApiParam(name = "ids",value = "资产模型Id数组",required = true)List<Integer> ids){

        return assetModelService.deleteAssetModel(ids);
    }

    @RequestMapping(value = "/selectAssetModel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询资产型号",notes = "条件查询资产型号信息",httpMethod = "post", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R selectAssetModel(@RequestBody @ApiParam(name = "assetModelVO",value = "资产型号",required = true) AssetModelVO assetModelVO){

        return assetModelService.selectAssetModel(assetModelVO);
    }
}
